import { createApp } from 'vue'
import { createPinia } from 'pinia'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import App from './App.vue'
import router from './router'
import './assets/styles/main.scss'
import { useUserStore } from '@/store/user'

const app = createApp(App)
const pinia = createPinia()


app.use(pinia)
app.use(router)
app.use(ElementPlus)

const userStore = useUserStore()
if (userStore.isLoggedIn) {
  userStore.fetchUserInfo().catch(error => {
    console.log('自动获取用户信息失败:', error)
    // 如果token失效，清除登录状态
    if (error.response?.status === 401) {
      userStore.logout()
    }
  })
}
// 注册Element Plus图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}


app.mount('#app')